//
//  BlipRequestUpdate.m
//  blipTest
//
//  Created by dawid on 08-12-12.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "BlipRequestUpdate.h"
#import "BlipURL.h"
#import "AppController.h"


@implementation BLP_URLRequestUpdate:NSMutableURLRequest 
@synthesize msgType;
@synthesize messageText;
@synthesize picturePath;
@synthesize recipient;
@synthesize imageData;
#pragma mark inits
-(id)initWithAuthString:(NSString*)authString
{
	NSURL *url = [NSURL BLP_initWithUpdateMessageUrl];
	if (![super initWithURL:url]){
		return nil;
	}	

	[super addValue:[NSString stringWithFormat:@"Basic %@",authString] forHTTPHeaderField:@"Authorization"];
	[super addValue:@"application/json" forHTTPHeaderField:@"Accept"];
	[super addValue:@"0.02" forHTTPHeaderField:@"X-Blip-Api"];	
	[super addValue:@"User-Agent" forHTTPHeaderField:@"ABlip"];
	[super setHTTPMethod:@"POST"];
	msgType = 1;

	return self;
}
-(id)initGetWithUrl:(NSString*)urlPath authotizationString:(NSString*)authString
{
	
	NSURL *url = [NSURL BLP_initWithURL:urlPath];
	if (![super initWithURL:url]){
		return nil;
	}	
	
	[super setValue:[NSString stringWithFormat:@"Basic %@", authString] forHTTPHeaderField:@"Authorization"];
	[super addValue:@"application/json" forHTTPHeaderField:@"Accept"];
	[super addValue:@"0.02" forHTTPHeaderField:@"X-Blip-api"];
	[super setHTTPMethod:@"GET"];
	msgType = 1;
	
	return self;
	
}
-(id)initGetMethodWithLogin:(NSString*)lastMsgId:(NSString*)authString
{
	
	NSURL *url = [NSURL BLP_initWithGetUpdatesUrl:lastMsgId];
	if (![super initWithURL:url]){
		return nil;
	}	
	
	[super setValue:[NSString stringWithFormat:@"Basic %@", authString] forHTTPHeaderField:@"Authorization"];
	[super addValue:@"application/json" forHTTPHeaderField:@"Accept"];
	[super addValue:@"0.02" forHTTPHeaderField:@"X-Blip-api"];
	[super setHTTPMethod:@"GET"];
	msgType = 1;

	return self;
	
}
-(id)initGetWithRelativePath:(NSString*)relativePath
{
	
	NSURL *url = [NSURL BLP_initWithRelativePath:relativePath];
	if (![super initWithURL:url]){
		return nil;
	}	
	//NSString *auth = [NSString stringWithFormat:@"Basic %@",LOGPASS_HASH];
	//[super addValue:auth forHTTPHeaderField:@"Authorization"];
	[super addValue:@"application/json" forHTTPHeaderField:@"Accept"];
	[super addValue:@"0.02" forHTTPHeaderField:@"X-Blip-api"];
	[super setHTTPMethod:@"GET"];
	msgType = 1;
	return self;
	
}
-(id)initGetWithURL:(NSString*)urlString
{
	
	NSURL *url = [NSURL BLP_initWithURL:urlString];
	if (![super initWithURL:url]){
		return nil;
	}	
	//NSString *auth = [NSString stringWithFormat:@"Basic %@",LOGPASS_HASH];
	//[super addValue:auth forHTTPHeaderField:@"Authorization"];
	[super addValue:@"application/json" forHTTPHeaderField:@"Accept"];
	[super addValue:@"0.02" forHTTPHeaderField:@"X-Blip-api"];
	[super setHTTPMethod:@"GET"];
	return self;
	
}

-(id)initDeleteMethodWithAuth:(NSString*)authString:(NSString*)relativePath
{

	NSURL *url = [NSURL BLP_initWithRelativePath:relativePath];
	if (![super initWithURL:url]){
		return nil;
	}	
	[super setValue:[NSString stringWithFormat:@"Basic %@", authString] forHTTPHeaderField:@"Authorization"];
	[super addValue:@"application/json" forHTTPHeaderField:@"Accept"];
	[super addValue:@"0.02" forHTTPHeaderField:@"X-Blip-api"];
	[super setHTTPMethod:@"DELETE"];
	return self;
	
}
-(id)initDirectedMsgWithAuthString:(NSString*)authString
{
	NSURL *url = [NSURL BLP_initWithDirectedMessageUrl];
	if (![super initWithURL:url]){
		return nil;
	}	

	[super addValue:[NSString stringWithFormat:@"Basic %@",authString] forHTTPHeaderField:@"Authorization"];
	[super addValue:@"application/json" forHTTPHeaderField:@"Accept"];
	[super addValue:@"0.02" forHTTPHeaderField:@"X-Blip-api"];
	[super addValue:@"User-Agent" forHTTPHeaderField:@"ABlip"];
	[super setHTTPMethod:@"POST"];
//	[super addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",BOUNDARY] forHTTPHeaderField:@"Content-Type"];
//	[super addValue:@"100-continue" forHTTPHeaderField:@"Expect"];
	msgType = 2;
	return self;
}
-(id)initPrivateMsgWithAuthString:(NSString*)authString
{
	
	NSURL *url = [NSURL BLP_initWithPrivateMessageUrl];
	if (![super initWithURL:url]){
		return nil;
	}	
	[super addValue:[NSString stringWithFormat:@"Basic %@",authString] forHTTPHeaderField:@"Authorization"];
	[super addValue:@"application/json" forHTTPHeaderField:@"Accept"];
	[super addValue:@"0.02" forHTTPHeaderField:@"X-Blip-api"];
	[super addValue:@"User-Agent" forHTTPHeaderField:@"ABlip"];
//	[super addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",BOUNDARY] forHTTPHeaderField:@"Content-Type"];
//	[super addValue:@"100-continue" forHTTPHeaderField:@"Expect"];
	[super setHTTPMethod:@"POST"];
	msgType = 3;
	return self;
}

//init for blipMessage downloads
-(id)initWithUpdateDownload:(NSString*)msgId authorizationString:(NSString*)authString
{
	NSURL *url = [NSURL BLP_initWithExtendedUpdateforMessageId:msgId];
	if ([super initWithURL:url]==nil){
#ifdef __DEBUG__

		NSLog(@"init w request jest nil");

#endif
		return nil;
	}	
	[super addValue:[NSString stringWithFormat:@"Basic %@",authString] forHTTPHeaderField:@"Authorization"];
	[super addValue:@"application/json" forHTTPHeaderField:@"Accept"];
	[super addValue:@"0.02" forHTTPHeaderField:@"X-Blip-Api"];	
	[super setHTTPMethod:@"GET"];
	msgType = 1;
	return self;
}
-(id)initWithDirectedDownload:(NSString*)msgId authorizationString:(NSString*)authString
{
	NSURL *url = [NSURL BLP_initWithExtendedDirectedforMessageId:msgId];
	if (![super initWithURL:url]){
		return nil;
	}	
	[super addValue:[NSString stringWithFormat:@"Basic %@",authString] forHTTPHeaderField:@"Authorization"];
	[super addValue:@"application/json" forHTTPHeaderField:@"Accept"];
	[super addValue:@"0.02" forHTTPHeaderField:@"X-Blip-Api"];	
	[super setHTTPMethod:@"GET"];
	msgType = 1;
	return self;
}
-(id)initWithPrivateDownload:(NSString*)msgId authorizationString:(NSString*)authString
{
	NSURL *url = [NSURL BLP_initWithExtendedPrivateforMessageId:msgId];
	if (![super initWithURL:url]){
		return nil;
	}	
	[super addValue:[NSString stringWithFormat:@"Basic %@",authString] forHTTPHeaderField:@"Authorization"];
	[super addValue:@"application/json" forHTTPHeaderField:@"Accept"];
	[super addValue:@"0.02" forHTTPHeaderField:@"X-Blip-Api"];	
	[super setHTTPMethod:@"GET"];
	msgType = 1;
	return self;
}
-(id)initWithSubscriptions:(NSString*)authString
{
	NSURL *url = [NSURL BLP_initWithSubscriptions];
	if (![super initWithURL:url]){
		return nil;
	}	
	
	[super addValue:[NSString stringWithFormat:@"Basic %@",authString] forHTTPHeaderField:@"Authorization"];
	[super addValue:@"application/json" forHTTPHeaderField:@"Accept"];
	[super addValue:@"0.02" forHTTPHeaderField:@"X-Blip-Api"];	
	[super setHTTPMethod:@"GET"];
	msgType = 1;
	
	return self;
}
#pragma label dealloc
-(void)dealloc
{

	if (messageText){
		[messageText release];
		messageText = nil;
	}
	if (picturePath){
		[picturePath release];
		picturePath = nil;
	}
	if (recipient){
		[recipient release];
		recipient = nil;
	}
	[super dealloc];
}
-(NSString*)description 
{
		return [[NSString alloc] initWithFormat:@"sending update message with text:%@, method:%@", messageText, [super HTTPMethod]];
}
#pragma mark Properties
-(NSString*)messageText
{
	return messageText;
}
-(void)setMessageText:(NSString*)text
{
	[text retain];
	[messageText release];
	messageText = text;
}
-(NSString*)picturePath
{
	return picturePath;
}
-(void)setPicturePath:(NSString*)path
{
	[path retain];
	[picturePath release];
	picturePath = path;
}
-(NSString*)recipient
{
	return recipient;
}
-(void)setRecipient:(NSString*)recipientName
{
	[recipientName retain];
	[recipient release];
	recipient = recipientName;
}
#pragma mark Methods

-(BOOL)setBody
{
	NSString *msgTypeString;
	if ([self msgType] ==1 ){
		msgTypeString = @"update";
	}else if ([self msgType] ==2){
		msgTypeString = @"directed_message";
	} else if (msgType ==3){
		msgTypeString = @"private_message";
	}
	
	NSMutableData* bodyData;
		
	//jesli jest rysunek nalezy stworzyc mime
	if (imageData || picturePath){
		[super addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",BOUNDARY] forHTTPHeaderField:@"Content-Type"];
		[super addValue:@"100-continue" forHTTPHeaderField:@"Expect"];
		NSString *text = [NSMutableString stringWithFormat: @"--%@\r\nContent-Disposition: form-data; name=\"%@[body]\"\r\n\r\n", BOUNDARY, msgTypeString];
		//dodanie textu
		bodyData = [NSMutableData dataWithData:[text dataUsingEncoding:NSUTF8StringEncoding]];
		if (messageText)
			[bodyData appendData:[NSData dataWithData:[messageText dataUsingEncoding:NSUTF8StringEncoding]]];
		//dodanie odbiorcy
		if (msgType!=1){
			text = [NSString stringWithFormat: @"\r\n--%@\r\nContent-Disposition: form-data; name=\"%@[recipient]\"\r\n\r\n", BOUNDARY, msgTypeString];
			[bodyData appendData:[text dataUsingEncoding:NSUTF8StringEncoding]];
			[bodyData appendData:[NSData dataWithData:[recipient dataUsingEncoding:NSUTF8StringEncoding]]];
		}
		
		text = [NSString stringWithFormat:@"\r\n--%@\r\nContent-Disposition: form-data; name=\"%@[picture]\"; filename=\"%@\"\r\nContent-Type: image/jpeg\r\n\r\n",BOUNDARY, msgTypeString,picturePath];
		[bodyData appendData:[text dataUsingEncoding:NSUTF8StringEncoding]];
		//dodanie obrazka
//		NSImage *img ;
		//if (imageData){
//			//[bodyData appendData:imageData];
//			img = [[NSImage alloc] initWithData:imageData];
//			//[bodyData appendData:[img TIFFRepresentation]];
//			NSString *path  = [AppController picturesDownloadDir];
//			path = [path stringByAppendingPathComponent:@"obrazek.jpg"];
//			NSFileManager *fm = [NSFileManager defaultManager];
//			NSError *error;
//			if ([fm createDirectoryAtPath:[path stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:&error]==NO){
#ifdef __DEBUG__

//				NSLog(@"Error while creating directory:%@", [error description]);

#endif	
//				return NO	;
//			} 
//			[fm createFileAtPath:path contents:[img TIFFRepresentation] attributes:nil];
//			
//			[bodyData appendData:[NSData dataWithContentsOfFile:path]];
//
//		}else {
		if (picturePath){
			NSData* pictureData = [NSData dataWithContentsOfFile:picturePath];
			[bodyData appendData:pictureData];
		}
//		}
		//if (img){
//			[img release];
//			img = nil;
//		}
		[bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", BOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]];
		[self setHTTPBody:bodyData];
		
	}else {
		NSMutableString* text = [NSMutableString stringWithFormat:@"%@[body]=", msgTypeString];
		
		if (!messageText){
#ifdef __DEBUG__

			NSLog(@"Empty messageText");

#endif
			return NO;
		}
		//dodanie tekstu z textBox'a
		if (messageText){
			[text appendString: messageText];
			
		}
		if (msgType!=1)
			[text appendFormat:@"&%@[recipient]=%@",msgTypeString,recipient];
#ifdef __DEBUG__

		NSLog(@"TEXT in setBody without picture:%@", text);

#endif
		bodyData = [NSData dataWithData:[text dataUsingEncoding:NSUTF8StringEncoding]];
		[self setHTTPBody:bodyData];
	}
	return YES;
}

@end